<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="翻译工具" prop="translateToolsType">
        <el-select v-model="queryParams.translateToolsType" placeholder="请选择翻译工具" clearable size="small">
          <el-option
            v-for="dict in translateToolsTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['amz:personalTranslate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amz:personalTranslate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['amz:personalTranslate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amz:personalTranslate:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <span style="font-size: 8px;vertical-align: sub;" >
          若存在多个正常账号可用，账号优先使用顺序：<span style="color: #9933ff;">正常>排序</span>；每次调用百度翻译后，系统会校验是否大于月翻译最大字符数，若是，则状态修改为暂停，每月1日系统自动修改为正常
        </span>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personalTranslateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="主键自增" align="center" prop="id" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100px;">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amz:personalTranslate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['amz:personalTranslate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="工具" align="center" prop="translateToolsType" width="60" :formatter="translateToolsTypeFormat" />
      <el-table-column label="APPID" align="center" prop="appid" show-overflow-tooltip/>
      <el-table-column label="密钥" align="center" prop="securityKey" show-overflow-tooltip/>
      <el-table-column label="状态" align="center" prop="status" width="60" :formatter="statusFormat" />
      <el-table-column label="排序(优先)" align="center" prop="translateSort" width="90"/>
      <el-table-column label="翻译最大字符数" align="center" prop="translateOneMaxCharNum" />
      <el-table-column label="月翻译最大字符数" align="center" prop="translateMonthMaxCharNum" />
      <el-table-column label="月翻译合计字符数" align="center" prop="translateMonthTotalCharNum" />
      <el-table-column label="创建人" align="center" prop="createBy" width="100"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改翻译工具对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="翻译工具" prop="translateToolsType">
          <el-select v-model="form.translateToolsType" placeholder="请选择翻译工具" :disabled="form.id!=null">
            <el-option
              v-for="dict in translateToolsTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="APPID" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入APPID" />
        </el-form-item>
        <el-form-item label="密钥" prop="securityKey">
          <el-input v-model="form.securityKey" placeholder="请输入密钥" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
              :disabled="dict.dictValue==1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序（优先）" prop="translateSort">
          <el-select v-model="form.translateSort" placeholder="请输入排序（优先使用1、2、3...）">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
            <el-option label="5" value="5"></el-option>
            <el-option label="6" value="6"></el-option>
            <el-option label="7" value="7"></el-option>
            <el-option label="8" value="8"></el-option>
            <el-option label="9" value="9"></el-option>
            <el-option label="10" value="10"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="翻译最大字符数" prop="translateOneMaxCharNum">
          <el-input v-model="form.translateOneMaxCharNum" placeholder="请输入翻译最大字符数" />
        </el-form-item>
        <el-form-item label="月翻译最大字符数" prop="translateMonthMaxCharNum">
          <el-input v-model="form.translateMonthMaxCharNum" placeholder="请输入月翻译最大字符数" />
        </el-form-item>
        <el-form-item label="月翻译合计字符数" prop="translateMonthTotalCharNum">
          <el-input v-model="form.translateMonthTotalCharNum" placeholder="请输入月翻译合计字符数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPersonalTranslate, getPersonalTranslate, delPersonalTranslate, addPersonalTranslate, updatePersonalTranslate, exportPersonalTranslate } from "@/api/amz/personalTranslate";

export default {
  name: "PersonalTranslate",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 翻译工具表格数据
      personalTranslateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 翻译工具字典
      translateToolsTypeOptions: [],
      // 状态字典
      statusOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        translateToolsType: null,
        status: null,
        createBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        translateToolsType: [
          { required: true, message: "翻译工具不能为空", trigger: "change" }
        ],
        appid: [
          { required: true, message: "APPID不能为空", trigger: "blur" }
        ],
        securityKey: [
          { required: true, message: "密钥不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();

    this.getDicts("qc_translate_tools_type").then(response => {
      this.translateToolsTypeOptions = response.data;
    });
    this.getDicts("qc_status").then(response => {
      this.statusOptions = response.data;
    });

  },
  methods: {
    /** 查询翻译工具列表 */
    getList() {
      this.loading = true;
      listPersonalTranslate(this.queryParams).then(response => {
        this.personalTranslateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 翻译工具字典翻译
    translateToolsTypeFormat(row, column) {
      return this.selectDictLabel(this.translateToolsTypeOptions, row.translateToolsType);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        translateToolsType: null,
        translateOneMaxCharNum: null,
        translateMonthMaxCharNum: null,
        translateMonthTotalCharNum: null,
        appid: null,
        securityKey: null,
        status: null,
        translateSort: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加翻译工具";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPersonalTranslate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改翻译工具";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePersonalTranslate(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPersonalTranslate(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除翻译工具编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPersonalTranslate(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有翻译工具数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPersonalTranslate(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
